import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { EntityTabsComponent } from '../../components/entity/entity-tabs.component';
import { VideoInfo } from '@app/shared/models/video.models';
import { CustomerConfigService } from '@app/core/public-api';

import { select } from '@ngrx/store';
import { User } from '@shared/models/user.model';
import { map } from 'rxjs/operators';

import { selectUserDetails } from '@core/auth/auth.selectors';
import { HttpClient } from '@angular/common/http';
import { AuthService } from '@app/core/auth/auth.service';

@Component({
  selector: 'tb-video-tabs',
  templateUrl: './video-tabs.component.html',
  styleUrls: []
})
export class VideoTabsComponent extends EntityTabsComponent<VideoInfo> {

  constructor(private http: HttpClient, protected store: Store<AppState>, private customerconfigService: CustomerConfigService) {
    super(store);
  }

  userDisplay = this.store.pipe(
    select(selectUserDetails),
    map((user) => this.getAuthorityName(user))
  );

  getAuthorityName(user: User): string {
    console.log("user", user)
    return user.name.split('@')[0];
  }
  ngOnInit() {
    super.ngOnInit();
    this.userDisplay.subscribe(e => {
      this.http.get("/api/getUserInfo",{headers:{"X-Authorization": "Bearer " + AuthService.getJwtToken()}}).subscribe((e) => {
        let userinfo=(e as any) ;
        userinfo.orgName=userinfo.user.principal.name.split('@')[0];
        this.customerconfigService.setConfig({ orgConfig:userinfo})
      })
    })
  }
}
